
import http, { baseHeaders, baseParams } from "@/utils/request";
import { UrlBindBankAccount, UrlDeleteBoundBankAccount, UrlFetchBoundBankAccount } from "@/utils/apiUrl";
import { isEmptyString } from "@/utils/utils";
import { Toast } from "antd-mobile";

//类型：请求成功
export const WALLET_LIST_REQ_SUCCESS = "WALLET_LIST_REQ_SUCCESS"

//类型：请求失败
export const WALLET_LIST_REQ_FAIL = "WALLET_LIST_REQ_FAIL"

//类型：请求失败
export const WALLET_LIST_REQ_CLEAR = "WALLET_LIST_REQ_CLEAR"


/**
 * 获取钱包列表
 * @returns thunk
 */
export const getWalletList = () => {
    return async (dispatch) => {
        //组装参数
        const params = baseParams()
        const headers = baseHeaders();

        //发起请求
        const res = await http.post(UrlFetchBoundBankAccount, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取获取钱包列表回调-结束：=======")

        //保存
        const success = (resCode === 0)
        dispatch(saveWalletListState(resObj, success))

        //如果有错误，提示
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
            }
        }
    }
}

/**
 * 新增钱包
 * @returns thunk
 */
export const bindWallet = (walletMobile, walletChannel, bindCallback) => {
    return async (dispatch) => {
        //组装参数
        const headers = baseHeaders();
        const params = baseParams()
        params.walletMobile = walletMobile || ""
        params.walletMobileConfirm = walletMobile || ""
        params.walletChannel = walletChannel || ""

        //发起请求
        const res = await http.post(UrlBindBankAccount, params, headers)
        const resCode = parseInt(res.code)
        console.log("新增钱包回调-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
            }
        }

        //回调
        bindCallback && bindCallback(success)
    }
}

/**
 * 删除钱包
 * @returns thunk
 */
export const deleteWallet = (ids, bindCallback) => {
    return async (dispatch) => {
        //组装参数
        const headers = baseHeaders();
        const params = baseParams()
        params.ids = ids || ""

        //发起请求
        const res = await http.post(UrlDeleteBoundBankAccount, params, headers)
        const resCode = parseInt(res.code)
        console.log("删除钱包回调-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
            }
        }

        //回调
        bindCallback && bindCallback(success)
    }
}

/**
 * 将 返回状态 信息保存到 Redux 中
 * @param {*} data 
 * @returns 
 */
export const saveWalletListState = (data, success) => {
    const result = {}
    if (success) {
        result.list = data
    } else {
        result.list = []
    }
    return {
        type: success ? WALLET_LIST_REQ_SUCCESS : WALLET_LIST_REQ_FAIL,
        payload: result
    }
}

/**
 * 将 返回状态 信息保存到 Redux 中
 * @param {*} data 
 * @returns 
 */
export const cleanLoanLimitState = () => {
    console.log("清空LoanLimit state: ")
    return {
        type: WALLET_LIST_REQ_CLEAR,
        payload: {}
    }
}
